<?php
App::import('Controller', 'Comments.Comments');
class AssetCommentsController extends CommentsController
{
	var $name = 'AssetComments';
	var $components = array('Session','Email','RequestHandler');
	protected $autoAdminActions = array('admin_bulkDelete','admin_bulkPublish','admin_bulkUnpublish');
	
	function beforeFilter()
	{
		parent::beforeFilter();
		$this->CommentAlias = 'Gallery comment';
	}
	
	function afteradd($lastInsertId,$itemId)
	{	
		$message = 'Your comment has been published.';
		$moderationOnComment = cr('GeneralConfig.moderate_gallery_comment');
		if($moderationOnComment == true)
		{
			$message = 'Your comment has been saved for moderation.';
		}else
		{
			$this->__applyNotification($lastInsertId,$itemId);
		}
		$this->successMsg(__($message));
		if(strpos($this->referer(),'listing_assets/view') || $this->RequestHandler->isAjax())
		{
			$this->Session->del('Message.ajax');
			Configure::write('debug',0);
			$this->layout = 'ajax';	
			echo __($message);
			exit;
		}				
		$this->redirect($this->referer());		
	}
	
	function __applyNotification($lastInsertId,$itemId)
	{			
		$presentComment = $this->AssetComment->findById($lastInsertId);
		$listingInfo = $this->AssetComment->ListingAsset->find('first',array('conditions'=>array('ListingAsset.id'=>$presentComment['AssetComment']['listing_asset_id']),'recursive'=>0));
		$presentComment = am($presentComment,$listingInfo);
		
		$viewUrl = Configure::read('cmsConfig.Backend.siteUrl').'/listing_assets/view/'.$presentComment['ListingAsset']['id'];
		$photoUrl = $viewUrl;
		$username = $listingInfo['ListingAsset']['poster']['display_name'];
		$commentator = $presentComment['AssetComment']['poster']['display_name'];
		
		$this->set(compact('photoUrl','username','commentator'));
		App::import('Vendor', 'EmailOperation');
			
		//***** code to get user whose photo get comments starts here*****//
			$userList2 = $this->AssetComment->User->find('list',array('fields'=>array('email','id'),'conditions'=>array('User.onPhotoGetComment'=>1),'recursive'=>-1));
			$useridToSendMail = $this->AssetComment->ListingAsset->field('user_id',array('ListingAsset.id'=>$presentComment['AssetComment']['listing_asset_id']));
			//$useridToSendMail = $listingInfo['ListingAsset']['user_id'];
			if($useridToSendMail != $presentComment['AssetComment']['user_id'])
			{				 
				if(!empty($useridToSendMail) && !empty($userList2))
				{
					
					$userList2 = array_flip($userList2);
					EmailOperation::sendMail('mailOnPhotoGetComment',array($this,$useridToSendMail,$presentComment,$userList2));		
				}
			}	
		//***** code to get user whose photo get comments ends here*****//
	
		//***** code for onCommentedPhotoComment starts here*****//
			$userList = $this->AssetComment->User->find('all',array('conditions'=>array('User.onCommentedPhotoComment'=>1),'recursive'=>-1));
			if($userList)
			{
				$userIdList = Set::extract('{n}.User.id',$userList);
				$sendingMailIds = $this->AssetComment->find('list',array('fields'=>array('id','user_id'),'conditions'=>array('AssetComment.listing_asset_id'=>$presentComment['AssetComment']['listing_asset_id'],'AssetComment.user_id'=>$userIdList)));
				$userList = Set::combine($userList,'{n}.User.id','{n}.User');
			}	
			
			if(!empty($sendingMailIds) && $presentComment['AssetComment']['status'] == AssetComment::PUBLISHED)
			{
				$sendingMailIds = 	array_diff($sendingMailIds,array($presentComment['AssetComment']['user_id']));
			}
			
			EmailOperation::sendMail('mailAfterCommentPublish',array($this,$sendingMailIds,$presentComment,$userList));
		//***** code for onCommentedPhotoComment ends here*****//
				
	}
	
	function admin_index()
	{
		$this->pageTitle = __('Gallery Comments');
		$this->helpers[] = 'Cms.Index';
		$this->paginate['limit'] = 10;
		$this->paginate['order'] = array('AssetComment.created DESC');
		$assetComments= $this->paginate();
		$this->set('assetComments',$assetComments);
	}
	
	function admin_edit($id)
	{
		$this->pageTitle = __('Edit');
		$this->AssetComment->recursive = -1;
		$commentData = $this->AssetComment->findById($id);
		if(empty($commentData))
		{
			$this->errorMsg(__("Invalid URL"));
			$this->redirect($this->referer());
		}
		if($this->data)
		{
			$this->data['AssetComment']['user_id'] = $commentData['AssetComment']['user_id'];
			$this->data['AssetComment']['listing_asset_id'] = $commentData['AssetComment']['listing_asset_id'];
			
			if($this->AssetComment->save($this->data))
			{
				$this->successMsg(__("Gallery comment has been updated."));
				$this->redirect(array('controller'=>'asset_comments','action'=>'admin_index'));
			}else
			{
				$this->errorMsg(__("Gallery comment could not be updated."));
			}
		}
		$this->data = $commentData;
		$this->set(compact('commentData'));
	}
	
	function add($item_id = null)
	{
		if(cr('GeneralConfig.captha_on_gallery_guest_comment') == true && isset($this->data['AssetComment']['captcha']))
		{
			$validCaptcha = $this->verifyCaptcha($this->data['AssetComment']['captcha']);
			if($validCaptcha == false)
			{			
				Configure::write('debug',0);
				$this->layout = 'ajax';
				exit;
			}
		}		
		parent::add($item_id);		
	}
	
}
?>